package org.kiwi.job.orm.service.impl;

import org.kiwi.job.orm.entity.Job;
import org.kiwi.job.orm.model.JobModel;
import org.kiwi.job.orm.repository.JobRepository;
import org.kiwi.job.orm.service.JobService;
import org.kiwi.util.mapping.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @email jack.liu.19910921@gmail.com
 * Created by jack on 17/2/14.
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private JobRepository jobRepository;

    /*
      * @Transactional is not necessary for the single atomic CRUD statement for better performance,
      * but you still have to take care of @Transactional for multi-statements scenario.
      * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
      */
    @Override
    public int deleteByPrimaryKey(Long id) {
        return jobRepository.deleteByPrimaryKey(id);
    }


    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
	 * but you still have to take care of @Transactional for multi-statements scenario.
	 * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
	 */
    @Override
    public JobModel findByPrimaryKey(Long id) {
        Job Job = jobRepository.selectByPrimaryKey(id);
        return beanMapper.map(Job, JobModel.class);
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int updateByPrimaryKey(JobModel JobModel) {
        return jobRepository.updateByPrimaryKey(beanMapper.map(JobModel, Job.class));
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int updateByPrimaryKeySelective(JobModel JobModel) {
        return jobRepository.updateByPrimaryKeySelective(beanMapper.map(JobModel, Job.class));
    }


    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int create(JobModel JobModel) {
        return jobRepository.insert(beanMapper.map(JobModel, Job.class));
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int createSelective(JobModel JobModel) {
        return jobRepository.insertSelective(beanMapper.map(JobModel, Job.class));
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int selectCount(JobModel JobModel) {
        return jobRepository.selectCount(beanMapper.map(JobModel, Job.class));
    }

    @Override
    public List<JobModel> findAllJob() {
        List<Job> jobList = jobRepository.findAllJob();
        return beanMapper.mapAsList(jobList, JobModel.class);
    }

    @Override
    public JobModel findByJobCode(String jobCode) {
        Job Job = jobRepository.selectByJobCode(jobCode);
        if (Job != null) {
            return beanMapper.map(Job, JobModel.class);
        }
        return null;
    }

}
